package com.app.biz.service.impl;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.app.biz.dto.BizDailyPowerConsumptionDto;
import com.app.common.core.domain.AjaxResult;
import com.app.common.utils.DateUtils;
import com.app.common.utils.SecurityUtils;
import com.app.common.utils.StringUtils;
import com.app.common.utils.bean.BeanUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizDailyPowerConsumptionMapper;
import com.app.biz.domain.formal.BizDailyPowerConsumption;
import com.app.biz.service.IBizDailyPowerConsumptionService;

/**
 * 每天耗电量Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-22
 */
@Service
public class BizDailyPowerConsumptionServiceImpl implements IBizDailyPowerConsumptionService
{
    @Autowired
    private BizDailyPowerConsumptionMapper bizDailyPowerConsumptionMapper;

    /**
     * 查询每天耗电量
     *
     * @param baseId 每天耗电量主键
     * @return 每天耗电量
     */
    @Override
    public BizDailyPowerConsumption selectBizDailyPowerConsumptionByBaseId(String baseId)
    {
        return bizDailyPowerConsumptionMapper.selectBizDailyPowerConsumptionByBaseId(baseId);
    }

    /**
     * 查询每天耗电量列表
     *
     * @param bizDailyPowerConsumption 每天耗电量
     * @return 每天耗电量
     */
    @Override
    public List<BizDailyPowerConsumption> selectBizDailyPowerConsumptionList(BizDailyPowerConsumption bizDailyPowerConsumption)
    {
        BizDailyPowerConsumptionDto dto = new BizDailyPowerConsumptionDto();
        BeanUtils.copyProperties(bizDailyPowerConsumption, dto);
        Date dayFlag = bizDailyPowerConsumption.getDayFlag();
        if(dayFlag != null){
            SimpleDateFormat yearFormat = new SimpleDateFormat(("yyyy"));
            SimpleDateFormat monthFormat = new SimpleDateFormat(("MM"));
            String yearStr = yearFormat.format(dayFlag);
            String monthStr = monthFormat.format(dayFlag);
            dto.setFilterYearFlag(Integer.parseInt(yearStr));
            dto.setFilterMonthFlag(Integer.parseInt(monthStr));
        }
        return bizDailyPowerConsumptionMapper.selectBizDailyPowerConsumptionList(dto);
    }

    /**
     * 新增每天耗电量
     *
     * @param bizDailyPowerConsumption 每天耗电量
     * @return 结果
     */
    @Override
    public int insertBizDailyPowerConsumption(BizDailyPowerConsumption bizDailyPowerConsumption)
    {
        bizDailyPowerConsumption.setBaseId(IdUtil.simpleUUID());
        bizDailyPowerConsumption.setBaseCreateTime(DateUtils.getNowDate());
        bizDailyPowerConsumption.setBaseCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return bizDailyPowerConsumptionMapper.insertBizDailyPowerConsumption(bizDailyPowerConsumption);
    }

    /**
     * 修改每天耗电量
     *
     * @param bizDailyPowerConsumption 每天耗电量
     * @return 结果
     */
    @Override
    public int updateBizDailyPowerConsumption(BizDailyPowerConsumption bizDailyPowerConsumption)
    {
        bizDailyPowerConsumption.setBaseUpdateTime(DateUtils.getNowDate());
        bizDailyPowerConsumption.setBaseUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return bizDailyPowerConsumptionMapper.updateBizDailyPowerConsumption(bizDailyPowerConsumption);
    }

    /**
     * 批量删除每天耗电量
     *
     * @param baseIds 需要删除的每天耗电量主键
     * @return 结果
     */
    @Override
    public int deleteBizDailyPowerConsumptionByBaseIds(String[] baseIds)
    {
        return bizDailyPowerConsumptionMapper.deleteBizDailyPowerConsumptionByBaseIds(baseIds);
    }

    /**
     * 删除每天耗电量信息
     *
     * @param baseId 每天耗电量主键
     * @return 结果
     */
    @Override
    public int deleteBizDailyPowerConsumptionByBaseId(String baseId)
    {
        return bizDailyPowerConsumptionMapper.deleteBizDailyPowerConsumptionByBaseId(baseId);
    }


    /**
     * @param bizDailyPowerConsumption
     * @return
     */
    @Override
    public AjaxResult updatePowerDay(BizDailyPowerConsumption bizDailyPowerConsumption) {
        if(StringUtils.isBlank(bizDailyPowerConsumption.getBaseId())){
            return AjaxResult.error("没有找到BaseId");
        }

        bizDailyPowerConsumption.setBaseUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        bizDailyPowerConsumption.setBaseUpdateTime(DateUtils.getNowDate());
        bizDailyPowerConsumptionMapper.updateBizDailyPowerConsumption(bizDailyPowerConsumption);
        return AjaxResult.success("更新成功");
    }

    /**
     * @param bizDailyPowerConsumption
     * @return
     */
    @Override
    public AjaxResult insertPowerDay(BizDailyPowerConsumption bizDailyPowerConsumption) {

        if(bizDailyPowerConsumption.getDayFlag() == null){
            return AjaxResult.error("请传入日期");
        }

        if(StringUtils.isBlank(bizDailyPowerConsumption.getDailyPowerConsumption())){
            return AjaxResult.error("请传入电量");
        }

        List<BizDailyPowerConsumption> result = bizDailyPowerConsumptionMapper.selectBizDailyPowerConsumptionListByRq(bizDailyPowerConsumption);

        if(!result.isEmpty()){
            return AjaxResult.warn("当前日期已存在数据，不可重复新增");
        }

        bizDailyPowerConsumption.setBaseId(IdUtil.simpleUUID());
        bizDailyPowerConsumption.setBaseCreateTime(DateUtils.getNowDate());
        bizDailyPowerConsumption.setBaseCreateBy(String.valueOf(SecurityUtils.getUserId()));
        bizDailyPowerConsumptionMapper.insertBizDailyPowerConsumption(bizDailyPowerConsumption);
        return AjaxResult.success("保存成功");
    }

    /**
     * 更新或者是插入每天的耗电量
     *
     * @param bizDailyPowerConsumption
     * @return
     */
    @Override
    public AjaxResult insertOrUpdateDay(BizDailyPowerConsumption bizDailyPowerConsumption) {
        if(StringUtils.isBlank(bizDailyPowerConsumption.getBaseId())){

            BizDailyPowerConsumption prefixSelectParams = new BizDailyPowerConsumption();
            prefixSelectParams.setDayFlag(bizDailyPowerConsumption.getDayFlag());
            Date dayFlag = bizDailyPowerConsumption.getDayFlag();
            BizDailyPowerConsumptionDto dto = new BizDailyPowerConsumptionDto();
            BeanUtils.copyProperties(prefixSelectParams, dto);
            if(dayFlag != null){
                SimpleDateFormat yearFormat = new SimpleDateFormat(("yyyy"));
                SimpleDateFormat monthFormat = new SimpleDateFormat(("MM"));
                String yearStr = yearFormat.format(dayFlag);
                String monthStr = monthFormat.format(dayFlag);
                dto.setFilterYearFlag(Integer.parseInt(yearStr));
                dto.setFilterMonthFlag(Integer.parseInt(monthStr));
            }

            List<BizDailyPowerConsumption> bizDailyPowerConsumptions = bizDailyPowerConsumptionMapper.selectBizDailyPowerConsumptionList(dto);
            if(bizDailyPowerConsumptions != null && !bizDailyPowerConsumptions.isEmpty()){
                return AjaxResult.warn("当前日期已经添加过数据");
            }

            bizDailyPowerConsumption.setBaseId(IdUtil.simpleUUID());
            bizDailyPowerConsumption.setBaseCreateTime(DateUtils.getNowDate());
            bizDailyPowerConsumption.setBaseCreateBy(String.valueOf(SecurityUtils.getUserId()));
        }else{
            //下面这两个创建时间和创建人在sql更新的时候并没有用，只是为了防止 ON DUPLICATE KEY UPDATE 语法报错
            bizDailyPowerConsumption.setBaseCreateTime(DateUtils.getNowDate());
            bizDailyPowerConsumption.setBaseCreateBy(String.valueOf(SecurityUtils.getUserId()));

            bizDailyPowerConsumption.setBaseUpdateTime(DateUtils.getNowDate());
            bizDailyPowerConsumption.setBaseUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        }
        bizDailyPowerConsumptionMapper.insertOrUpdateDay(bizDailyPowerConsumption);
        return AjaxResult.success("保存成功");
    }
}
